package com.opennews.openplatform.controller;

import com.opennews.openplatform.dto.account.group.user.QuitAccountGroupUserDto;
import com.opennews.openplatform.dto.account.group.user.RemoveAccountGroupUserDto;
import com.opennews.openplatform.myspringbootcore.dto.ApiResponse;
import com.opennews.openplatform.myspringbootcore.security.MyClaims;
import com.opennews.openplatform.myspringbootmvccore.helper.HttpResponseHelper;
import com.opennews.openplatform.myspringbootmvccore.security.RequestIdentityExtractor;
import com.opennews.openplatform.service.AccountGroupUserService;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RequiredArgsConstructor
@RestController
@RequestMapping("/account-group-user")
public class AccountGroupUserController {
    private final AccountGroupUserService accountGroupUserService;
    private final RequestIdentityExtractor requestIdentityExtractor;
    private final HttpResponseHelper httpResponseHelper;

    @GetMapping("/query-available-account-groups")
    public ResponseEntity<ApiResponse<List<Map<String, Object>>>> queryAvailableAccountGroups() {
        MyClaims claims = requestIdentityExtractor.getClaims();
        String userId = claims.id;
        List<Map<String, Object>> result = accountGroupUserService.queryAvailableAccountGroups(userId);

        return httpResponseHelper.getSuccessResponse(result);
    }

    @PostMapping("/remove")
    public ResponseEntity<ApiResponse<Object>> remove(@Valid @RequestBody RemoveAccountGroupUserDto data) {
        MyClaims claims = requestIdentityExtractor.getClaims();
        String accountGroupId = claims.accountGroupId;
        String userId = data.getUserId();
        String username = data.getUsername();
        accountGroupUserService.remove(accountGroupId, userId, username);

        return httpResponseHelper.getSuccessResponse();
    }

    @PostMapping("/quit")
    public ResponseEntity<ApiResponse<Object>> quit(@Valid @RequestBody QuitAccountGroupUserDto data) {
        String accountGroupId = data.getAccountGroupId();
        MyClaims claims = requestIdentityExtractor.getClaims();
        String userId = claims.id;
        String username = claims.username;
        accountGroupUserService.remove(accountGroupId, userId, username);

        return httpResponseHelper.getSuccessResponse();
    }
}
